package com.xjh.mange.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjh.mange.common.constant.UserSessionName;
import com.xjh.mange.common.util.SessionUtil;
import com.xjh.mange.model.Goods;
import com.xjh.mange.model.User;
import com.xjh.mange.service.GoodsService;
import com.xjh.mange.service.LogService;
import com.xjh.mange.vo.LayuiTableVo;
import com.xjh.mange.vo.ResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author 徐佳辉
 * @date 2020/12/19 20:43
 */
@Controller
@RequestMapping("/goods")
@Slf4j
public class GoodsController {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private LogService logService;


    /**
     * 分页
     *
     * @param page
     * @param limit
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public LayuiTableVo list(int page, int limit, String name, HttpSession session) {
        if (page < 1) {
            page = 1;
        }
        if (limit < 1) {
            limit = 5;
        }
        IPage<Goods> page1;
        if (name != null) {
            page1 = goodsService.pageByName(page, limit, name);
        } else {
            page1 = goodsService.page(page, limit);
        }
        return LayuiTableVo.ok(page1.getRecords(), (int) page1.getTotal());
    }

    /**
     * 添加或更新物品
     *
     * @param goods
     * @return
     */
    @PostMapping("/insert")
    @ResponseBody
    public ResponseVO insert(@RequestBody Goods goods, HttpSession session) {
        User user = (User) session.getAttribute(UserSessionName.USER);
        if (user == null) {
            return ResponseVO.error("请登录");
        }
        log.info("goods:[{}]", goods);
        if (goods.getGoodId() != null) {
            goods.setUpdateTime(new Date());
            logService.Log(SessionUtil.getName(session), "更新物品:" + goods.getGoodName());
            boolean b = goodsService.updateById(goods);
            return b ? ResponseVO.ok("更新成功") : ResponseVO.error("更新失败");
        } else {
            goods.setUserId(user.getUserId());
            goods.setCreateTime(new Date());
            goods.setUpdateTime(new Date());
            logService.Log(SessionUtil.getName(session), "添加物品:" + goods.getGoodName());
            boolean save = goodsService.save(goods);
            return save ? ResponseVO.ok("添加成功") : ResponseVO.error("添加失败");
        }
    }

    /**
     * 单个删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteOne")
    @ResponseBody
    public ResponseVO delete(Integer id, HttpSession session) {
        logService.Log(SessionUtil.getName(session), "删除物品");
        boolean removeById = goodsService.removeById(id);
        return removeById ? ResponseVO.ok("删除成功") : ResponseVO.error("删除失败");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/deletes")
    @ResponseBody
    public ResponseVO delete(String ids, HttpSession session) {
        logService.Log(SessionUtil.getName(session), "批量删除物品");
        log.info("id字符串,[{}]", ids);
        String[] s = ids.split(",");
        List<String> list = Arrays.asList(s);
        boolean b = goodsService.removeByIds(list);
        return b ? ResponseVO.ok("删除成功") : ResponseVO.error("删除失败");

    }

}
